home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
DEV
/
C-H
/
Finder Progress C.cpt
/
Finder Progress folder
/
Finder Progress.c
next >
Wrap
C/C++ Source or Header
|
1993-01-02
|
5KB
|
212 lines
/*
Finder Progress
by Joe Zobkiw
This is a simple THINK C 5.0.4 project and compiled application
that shows you how to (very easily) implement a Finder 7-like
progress meter. I have only seen cheap imitations on the nets so
I decided to code one that looks a little closer to how the Finder
does.
You can extend this code in any way you wish, make it movable modal,
do with it what you will, it should work in almost any scenario.
If you make any changes please send them to me at any of the
following addresses. Enjoy, and please give this to your friends.
America Online: AFL Zobkiw
Internet: zobkiw@world.std.com
*/
#include "Finder Progress.h"
main()
{
DialogPtr d = nil;
GrafPtr savePort = nil;
Rect r;
short i;
short bitDepth;
long someTime;
/* initialize the toolbox */
InitGraf(&thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
InitCursor();
GetDateTime(&randSeed);
FlushEvents(everyEvent,0);
DrawMenuBar();
/* show the about alert */
Alert(kAboutAlertID, nil);
/* get the dialog box */
d = GetNewDialog(kModalProgressDialogID, nil, (WindowPtr)-1);
if (d == nil) ExitToShell();
/* set up the port for drawing */
GetPort(&savePort);
SetPort(d);
ParamText("\pNow doing something time consuming…","\p","\p","\p");
ShowWindow(d);
DrawDialog(d);
/* if this isn’t available, we are toast anyway! */
SetCursor(*GetCursor(watchCursor));
/* get the rectangle of the progress item and the current bitdepth */
r = DItemRect(d, kDialogProgressItem);
bitDepth = BitDepth();
/* now we’re making progress! */
for (i=0;i<=100;++i) {
UpdateProgress(&r, i, (bitDepth >= kColorBitDepth));
Delay(2, &someTime);
}
/* clean things up */
InitCursor();
ParamText("\p","\p","\p","\p");
DisposeDialog(d);
SetPort(savePort);
}
/********************************************************************
UpdateProgress
draws the progress item, expects the port to be set properly.
You can pass any parameters you like into this routine, it
depends on the situation you plan on using it in. I decided
to simply pass a Rect, percentage, and a color flag.
this implementation draws the entire thermometer each time
which will cause the cursor to flicker if you move it over the
thermometer itself. i do this for a few reasons, one being i
didn't want the overhead or hassle of allocating an offscreen
pixmap or bitmap to provide flicker free animation, and secondly
if we are drawing and a screensaver goes on, when it kicks out
again we will be sure to draw the entire thermometer immediately
and not just a portion of it.
********************************************************************/
void UpdateProgress(Rect *r, short percentage, Boolean color)
{
RGBColor rgb;
Rect rect, fillRect, emptyRect;
PenState savePen;
RGBColor foreColor;
short offset;
rect = fillRect = emptyRect = *r;
/* save the pen state and color */
GetPenState(&savePen);
if (color)
GetForeColor(&foreColor);
PenNormal();
/* first frame the rectangle */
if (color) {
rgb.red = 0;
rgb.green = 0;
rgb.blue = 0;
RGBForeColor(&rgb);
} else {
ForeColor(blackColor);
}
FrameRect(&rect);
/* figure out the percentage */
InsetRect(&rect, 1, 1);
InsetRect(&fillRect, 1, 1);
InsetRect(&emptyRect, 1, 1);
offset = (rect.right - rect.left) * percentage / 100;
/* first draw the filled in portion */
fillRect.right = fillRect.left + offset;
if (color) {
rgb.red = kGrayProgressColorRed;
rgb.green = kGrayProgressColorGreen;
rgb.blue = kGrayProgressColorBlue;
RGBForeColor(&rgb);
} else {
ForeColor(blackColor);
}
PaintRect(&fillRect);
/* now draw the unfilled portion */
emptyRect.left = fillRect.right + 1;
if (color) {
rgb.red = kPurpleProgressColorRed;
rgb.green = kPurpleProgressColorGreen;
rgb.blue = kPurpleProgressColorBlue;
RGBForeColor(&rgb);
} else {
ForeColor(whiteColor);
}
PaintRect(&emptyRect);
/* restore pen settings, colors, restore to black when not in color */
SetPenState(&savePen);
if (color) {
RGBForeColor(&foreColor);
} else {
ForeColor(blackColor);
}
}
/********************************************************************
DItemRect
returns the rectangle of a dialog item
********************************************************************/
Rect DItemRect(DialogPtr dialog, short item)
{
short itemType;
Handle itemHandle;
Rect itemRect;
GetDItem(dialog, item, &itemType, &itemHandle, &itemRect);
return itemRect;
}
/********************************************************************
ColorQDIsPresent
returns true if color quickdraw is present, false otherwise
********************************************************************/
Boolean ColorQDIsPresent(void)
{
SysEnvRec theWorld;
SysEnvirons(1, &theWorld);
return(theWorld.hasColorQD);
}
/********************************************************************
BitDepth
returns the bitdepth of the main monitor
********************************************************************/
short BitDepth(void)
{
short bits = 1;
if (ColorQDIsPresent()) {
GDHandle mainDevice;
PixMapHandle pixMap;
mainDevice = GetMainDevice();
pixMap = (**mainDevice).gdPMap;
bits = (**pixMap).pixelSize;
}
return bits;
}